package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import qlsl.androiddesign.activity.subactivity.BannerCategoryActivity;
import qlsl.androiddesign.activity.subactivity.CategoryListActivity;
import qlsl.androiddesign.adapter.subadapter.BannerCategoryAdapter;
import qlsl.androiddesign.entity.otherentity.HomeBanner;
import qlsl.androiddesign.entity.otherentity.ShoopChildList;
import qlsl.androiddesign.http.service.commonservice.HomeService;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 轮播一级分类页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class BannerCategoryView extends FunctionView<BannerCategoryActivity> {

	private ListView listView;

	private List<ShoopChildList.RowsBean> list = new ArrayList<ShoopChildList.RowsBean>();

	public BannerCategoryView(BannerCategoryActivity activity) {
		super(activity);
		setContentView(R.layout.activity_banner_category);
	}

	protected void initView(View view) {
		setTitle("分类");
		listView = findViewById(R.id.listView);
	}

	protected void initData() {
		queryCategory();
	}

	protected void initListener() {

	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof List) {
			List<ShoopChildList.RowsBean> list_result = (List<ShoopChildList.RowsBean>) t[0];
			resetList(list_result);
			notifyDataSetChanged();
		}

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.list_item:
			doClickListItem(view);
			break;
		}
	}

	private void resetList(List<ShoopChildList.RowsBean> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	private void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) listView.getAdapter();
		if (adapter == null) {
			adapter = new BannerCategoryAdapter(activity, list);
			listView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
	}

	public void queryCategory() {
		HomeBanner item = (HomeBanner) activity.getIntent().getSerializableExtra("data");
		HomeService.queryCategoryList(item.getId(), this, activity);
	}

	private void doClickListItem(View view) {
		int position = listView.getPositionForView(view);
		ShoopChildList.RowsBean item = list.get(position);
		startActivity(item, CategoryListActivity.class);
	}
}
